/**
 * 
 * 
 * @param nums 
 */

function semiOrderedPermutation(nums: number[]): number {
    const len = nums.length
    const index_first = nums.indexOf(1)
    const index_last = nums.indexOf(len)
    const change = index_last < index_first ? 1 : 0
    console.table([['len', len], ['index_first', index_first], ['index_last', index_last], ['change', change]])
    return index_first + len - 1 - index_last - change
};

const nums = [2,1,4,3]
console.log(semiOrderedPermutation(nums))